const ipUtil = require("../../utils/hostUtil")
const requestUtil = require("../../utils/requestUtil")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNo: 1,
        pageSize: 4,
        list: [],
        fruitName:''
    },
    onShareAppMessage: function () {
        return {
            title: '鲜果惠',
            desc: '',
            path: 'pages/index/index',
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: '分享成功',
                    icon: "none"
                });
            },
            fail: function (res) {
                // 转发失败
                wx.showToast({
                    title: '分享失败',
                    icon: "none"
                })
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },
    /**
     * 添加购物车
     */
    add_to_cart: function (goodsId) {
        wx.request({
            url: `${ipUtil.host}${requestUtil.addcart}`,
            data: {"id": goodsId},
            header: requestUtil.header,
            success(res) {
                if (res.success) {
                    wx.showToast({
                        title: '添加成功',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    wx.showToast({
                        title: res.messages,
                        icon: 'none',
                        duration: 1500
                    })
                }
            }
        })
    },
    to_goods_details: function (e) {
        var goods_id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/detail/detail?id=${goods_id}`
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.showLoading({
            title: '加载中请稍后',
            icon: 'loading',
            duration: 2000
        })
        this.loadPage();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    /**
     * 请求页面数据
     */
    loadPage:function (fruitName) {
        console.log(fruitName)
        wx.showLoading({
            title: '加载中',
        })
        fruitName = fruitName || "";
        let that=this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.goodsList}`,
            data: {fruitName: fruitName},
            header: requestUtil.header,
            method: "POST",
            success(res) {
				console.log(res)
                if (res.data.rows) {
                    wx.hideLoading();
                    that.setData({
                        list: res.data.rows
                    })
                }
            }
        })
    },
    search:function (){
        // var that = this
        // let fruitName= that.fruitName
        // console.log(fruitName)
        console.log(111)
        this.loadPage(this.data.fruitName);
    },change_fruit_name:function (e){
        console.log(e.detail.value)
        this.setData({
            fruitName: e.detail.value
        })
    }
})